package com.aofeng.hybrid.orm;

import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteCursor;

/**
 * 表的列
 * @author LGY
 *
 */
public class Column
{
    /**
     * 范化类型，日期类型均作为日期看待
     * @param type
     * @return
     */
    public static String normalizeType(String type)
    {
        String tp = type.toLowerCase();
        if(tp.equals("string") || tp.equals("date") || tp.equals("time"))
        {
            return "TEXT";
        }
        else if(tp.equals("integer"))
        {
            return "INTEGER";
        }
        else if(tp.equals("boolean")) {
            return "NUMERIC";
        }
        else if(tp.equals("double") || tp.equals("big_decimal"))
        {
            return "REAL";
        }
        else if(tp.equals("blob"))
        {
            return "BLOB";
        }
        else
            return "TEXT";
    }

    /**
     * 格式化值
     * @param type
     * @param value
     * @return
     */
    public static String normalizeValue(String type, String value) {
        if(value == null)
            return "NULL";
        type = type.toLowerCase();
        if (type.equals("string") || type.equals("date") || type.equals("time"))
            return "'" + value + "'";
        else
            return value;
    }

    /**
     * 得到查询列的值
     * @param cursor
     * @param idx
     * @return
     */
    public static Object normalizeValue(Cursor cursor, int idx) {
        SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
        CursorWindow cursorWindow = sqLiteCursor.getWindow();
        int pos = cursor.getPosition();
        int type = -1;
        if (cursorWindow.isNull(pos, idx)) {
            return null;
        } else if (cursorWindow.isString(pos, idx)) {
            return "\"" + cursor.getString(idx) + "\"";
        } else if (cursorWindow.isFloat(pos, idx)) {
            return cursor.getFloat(idx);
        } else if (cursorWindow.isLong(pos, idx)) {
            return cursor.getInt(idx);
        } else {
            throw new RuntimeException("不支持的数据类型。");
        }
    }

    /**
     * 是不是字段可以换类型
     * @param type
     * @param type2
     * @return
     */
    public static boolean isCompatible(String type, String type2)
    {
        return true;
    }
}

